package learnjdk.datetime;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

/**
 * Created by alex on 2017/7/16.
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        LocalDate date = LocalDate.of(2017, 7, 16);
        System.out.println(date.getYear());
        System.out.println(date.getDayOfYear());
        System.out.println(date.getDayOfWeek().name());

        System.out.println("=====================");

        date = LocalDate.now();
        System.out.println(date);
        System.out.println(date.getYear());
        System.out.println(date.getDayOfYear());
        System.out.println(date.getMonthValue());

        System.out.println("======================");

        System.out.println(date.get(ChronoField.DAY_OF_MONTH));

        System.out.println("======================");

        System.out.println(LocalDate.parse("2017-07-16"));

        System.out.println("======================");

        System.out.println(LocalDateTime.of(2017, 7, 16, 10, 9));

        System.out.println("======================");

        // Period
        // days 级别居然是日期相见，晕死
        Period period = Period.between(LocalDate.of(2017, 1, 1), LocalDate.now());
        System.out.println(period.getDays());
        System.out.println(period.get(ChronoUnit.DAYS));

        System.out.println("======================Duration");

        // Duration
        Duration duration = Duration.between(LocalTime.of(10, 23), LocalTime.now());
        System.out.println(duration.getSeconds());
        System.out.println(duration.getNano());
        System.out.println(duration.toDays());

        System.out.println("======================");

        date = date.plusWeeks(1);
        System.out.println(date);
        date = date.plusMonths(7);
        System.out.println(date);

        System.out.println("======================");

        ZoneId romeZone = ZoneId.of("Europe/Rome");
        ZoneId defaultZoneId = TimeZone.getDefault().toZoneId();
        System.out.println(romeZone);
        System.out.println(defaultZoneId);
        System.out.println(defaultZoneId.equals(ZoneId.systemDefault()));

        System.out.println("======================");

        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println(dateTime.toInstant(ZoneOffset.UTC).getEpochSecond());

        System.out.println("======================");

        final Clock clock = Clock.systemUTC();
        System.out.println(clock.instant());
        System.out.println(clock.millis() == System.currentTimeMillis());
        System.out.println(clock.millis());
        TimeUnit.SECONDS.sleep(1);
        System.out.println(clock.millis());
        TimeUnit.SECONDS.sleep(1);
        System.out.println(clock.millis());
        TimeUnit.SECONDS.sleep(1);
        System.out.println(clock.millis());

        System.out.println("======================format");
        // format
        System.out.println(date.format(DateTimeFormatter.ISO_DATE));
        System.out.println(LocalDate.parse("2017-10-11"));
    }
}
